package com.group2.operations.service.impl;

import com.group2.operations.common.api.ApiResult;
import com.group2.operations.mapper.AdminMapper;
import com.group2.operations.mapper.FlowMapper;
import com.group2.operations.model.Admin;
import com.group2.operations.service.AdminService;
import liquibase.pro.packaged.O;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private TaskService taskService;
    @Autowired
    private FlowMapper flowMapper;

    @Override
    public Admin queryUsernameByName(String username) {

        return adminMapper.queryUsernameByName(username);
    }

    @Override
    public List<Map<String, Object>> adminDepCheck(){
        List<Task> tasklist = taskService.createTaskQuery().taskAssignee("department").list();
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
        for (Task task : tasklist){
            Map<String,Object> map1 = new HashMap<String,Object>();
            Map<String, Object> variables = taskService.getVariables(task.getId());
            variables.put("taskName", task.getName());
            map1.put(task.getId(), variables);
            listMap.add(map1);
        }
        return listMap;
    }

    @Override
    public List<Map<String, Object>> adminCoCheck(){
        List<Task> tasklist = taskService.createTaskQuery().taskAssignee("corperation").list();
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
        for (Task task : tasklist){
            Map<String,Object> map1 = new HashMap<String,Object>();
            Map<String, Object> variables = taskService.getVariables(task.getId());
            variables.put("taskName", task.getName());
            map1.put(task.getId(), variables);
            listMap.add(map1);
        }
        return listMap;
    }

    @Override
    @Transactional
    public ApiResult adminDepTrack(String username,String employee_num,String taskId,String action){
        Map<String ,Object> map = taskService.getVariables(taskId);
        String processId = (String) map.get("processId");
        //通过或驳回
        Map<String, Object> variables = new HashMap<>();
        variables.put("departmentOutcome", action);
        taskService.complete(taskId, variables);

        //数据库状态更新
        String state = (action.equals("通过") ? "部门审核通过" : "部门审核驳回")+" "+"审核人员："+username+" 工号："+employee_num;
        flowMapper.adminDepInsertFlowState(processId, state, new Date());
        String msg = username+"进行了审核";
        return ApiResult.success(msg);
    }

    @Override
    @Transactional
    public ApiResult adminCoTrack(String username,String employee_num,String taskId,String action){
        Map<String ,Object> map = taskService.getVariables(taskId);
        String processId = (String) map.get("processId");
        //通过或驳回
        Map<String, Object> variables = new HashMap<>();
        variables.put("corperationOutcome", action);
        taskService.complete(taskId, variables);

        //数据库状态更新
        String state = (action.equals("通过") ? "公司审核通过" : "公司审核驳回")+" "+"审核人员："+username+" 工号："+employee_num;
        flowMapper.adminCoInsertFlowState(processId, state, new Date());
        String msg = username+"进行了审核";
        return ApiResult.success(msg);
    }
}
